from instructions import Instructions

def machinecode(code_list) -> bytes:
    s = b""

    for code in code_list:
        match code[0]:
            case "str":
                text = code[1]
                s += text.encode("utf-8")
                s += b"\0"
            case "db":
                s += code[1].to_bytes(1, "little")
            case 'dw':
                s += code[1].to_bytes(2, "little")
            case "dd":
                s += code[1].to_bytes(4, "little")
            case "dq":
                s += code[1].to_bytes(8, "little")

            case "exit":
                s += Instructions["exit"].to_bytes(1)
                s += b'\0\0'
            case "mov_ir0":
                s += Instructions["mov_ir0"].to_bytes(1)
                s += code[1].to_bytes(2, "little")
            case "mov_ir1":
                s += Instructions["mov_ir1"].to_bytes(1)
                s += code[1].to_bytes(2, "little")
            case "mov_ir2":
                s += Instructions["mov_ir2"].to_bytes(1)
                s += code[1].to_bytes(2, "little")
            case "mov_ir3":
                s += Instructions["mov_ir3"].to_bytes(1)
                s += code[1].to_bytes(2, "little")
            case "mov_i0m":
                s += Instructions["mov_i0m"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mov_r0r0":
                s += Instructions["mov_r0r0"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mov_rr":
                s += Instructions["mov_rr"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mov_r0m":
                s += Instructions["mov_r0m"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mov_r1m":
                s += Instructions["mov_r1m"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mov_r2m":
                s += Instructions["mov_r2m"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mov_r3m":
                s += Instructions["mov_r3m"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mov_r4m":
                s += Instructions["mov_r4m"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mov_r5m":
                s += Instructions["mov_r5m"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mov_r6m":
                s += Instructions["mov_r6m"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mov_r7m":
                s += Instructions["mov_r7m"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mov_rm":
                s += Instructions["mov_rm"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mov_mr0":
                s += Instructions["mov_mr0"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mov_mr1":
                s += Instructions["mov_mr1"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mov_mr2":
                s += Instructions["mov_mr2"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mov_mr3":
                s += Instructions["mov_mr3"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mov_mr4":
                s += Instructions["mov_mr4"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mov_mr5":
                s += Instructions["mov_mr5"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mov_mr6":
                s += Instructions["mov_mr6"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mov_mr7":
                s += Instructions["mov_mr7"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mov_mm":
                s += Instructions["mov_mm"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "swap_rr":
                s += Instructions["swap_rr"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "swap_mm":
                s += Instructions["swap_mm"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "swap_r_00":
                s += Instructions["swap_r_00"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "swap_r_01":
                s += Instructions["swap_r_01"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "swap_r_02":
                s += Instructions["swap_r_02"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "swap_r_03":
                s += Instructions["swap_r_03"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "swap_r_04":
                s += Instructions["swap_r_04"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "swap_r_05":
                s += Instructions["swap_r_05"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "swap_r_06":
                s += Instructions["swap_r_06"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "swap_r_07":
                s += Instructions["swap_r_07"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "band":
                s += Instructions["band"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "bor":
                s += Instructions["bor"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "bxor":
                s += Instructions["bxor"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "bnot":
                s += Instructions["bnot"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "bleft":
                s += Instructions["bleft"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "bright":
                s += Instructions["bright"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "add":
                s += Instructions["add"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "sub":
                s += Instructions["sub"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "mul":
                s += Instructions["mul"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "div":
                s += Instructions["div"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "cmp":
                s += Instructions["cmp"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "jmp":
                s += Instructions["jmp"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += b'\0'
            case "jmp_a_if_b":
                s += Instructions["jmp_a_if_b"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "jmp_a_ifn_b":
                s += Instructions["jmp_a_ifn_b"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "jmp_adr_ife":
                s += Instructions["jmp_adr_ife"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "jmp_adr_ifne":
                s += Instructions["jmp_adr_ifne"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "jmp_adr_ifalb":
                s += Instructions["jmp_adr_ifalb"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "jmp_adr_ifagb":
                s += Instructions["jmp_adr_ifagb"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "jmp_adr_ifaleb":
                s += Instructions["jmp_adr_ifaleb"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "jmp_adr_ifageb":
                s += Instructions["jmp_adr_ifageb"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "i_char":
                s += Instructions["i_char"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += b'\0'
            case "map_dm":
                s += Instructions["map_dm"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")
            case "umap_d":
                s += Instructions["umap_d"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += b'\0'
            case "info_dev_count":
                s += Instructions["info_dev_count"].to_bytes(1)
                s += b'\0\0'
            case "call_d":
                s += Instructions["call_d"].to_bytes(1)
                s += code[1].to_bytes(1, "little")
                s += code[2].to_bytes(1, "little")

    return s
